<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <textarea name="" id="" cols="30" rows="10" v-model.trim="textarea" @keyup.enter="handlerClick"></textarea>
        <button @click="tianjia">发布评论</button>
        <!-- 渲染用户的评论 -->
        <div v-for="(item,index) in list">
            <p>{{ item.time }}>>{{ item.name }}>>{{ item.contents }}----{{ item.count }}
                <button @click="dianzan(item,index)">点赞</button>
                <button @click="zhiding(item,index)">置顶</button>
                <button @click="shanchu(index)">删除</button>
            </p>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script>
        let mm = new Vue({
            el:"#app",
            data:{
                list: [{
                        id: 1,
                        name: '茄子',
                        contents: '住在山里的感觉整不错!',
                        count: 0,
                        time: '2021-10-11',
                        good:true
                    },
                    {
                        id: 2,
                        name: 'jy',
                        contents: '今天天气真不错! 是个打csgo的好日子!',
                        count: 0,
                        time: '2021-10-12',
                        good:true
                    },
                ],
                ipt:"",
                textarea:'',
            },
            methods:{
                tianjia(){
                    let shijian = new Date()
                    let obj={};
                    obj.name = "爸爸";
                    obj.time = `${shijian.getFullYear()}-${shijian.getMonth()+1}-${shijian.getDate()}` ;
                    obj.count = 0;
                    obj.good = true;
                    obj.id = this.list.length+1;
                    obj.contents = this.textarea;
                    if(this.textarea != ""){
                        this.list.push(obj)
                    }else{
                        alert("请输入评论内容")
                    }
                    this.textarea="";
                    console.log(obj);
                },
                dianzan(a,b){
                    a.count++
                },
                zhiding(a,b){
                    this.list.splice(b,1)
                    this.list.unshift(a)
                },
                shanchu(a){
                    this.list.shift(a,1)
                },
                handlerClick(){
                    let shijian = new Date()
                    let obj={};
                    obj.name = "爸爸";
                    obj.time = `${shijian.getFullYear()}-${shijian.getMonth()+1}-${shijian.getDate()}` ;
                    obj.count = 0;
                    obj.good = true;
                    obj.id = this.list.length+1;
                    obj.contents = this.textarea;
                    if(this.textarea != ""){
                        this.list.push(obj)
                    }else{
                        alert("请输入评论内容")
                    }
                    this.textarea="";
                    console.log(obj);
                }
            }
        })
    </script>
</body>
</html>